package com.rc.evcharger.service.order;

import com.rc.evcharger.common.dto.ChargeBalaceDto;
import com.rc.evcharger.mapper.order.OrderDelayPushMapper;
import com.rc.evcharger.mapper.order.OrderInfoMapper;
import com.rc.evcharger.model.order.OrderDelayPush;
import com.rc.evcharger.model.order.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Create by Yang on 2019/12/14
 */
@Service
public class OrderDelayPushServiceImpl implements OrderDelayPushService {

    @Autowired
    private OrderDelayPushMapper orderDelayPushMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public OrderDelayPushMapper getMapper() {
        return orderDelayPushMapper;
    }


    @Override
    public OrderDelayPush selectOrderNum(String orderNum) {
        return orderDelayPushMapper.selectOrderNum(orderNum);
    }

    @Override
    public void addOrderDelayPush(ChargeBalaceDto cbdto, Long elecPileId, Long orderId) {
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);

        //补推时间-订单结束时间<30秒 不做保存
        if (new Date().getTime() - orderInfo.getEndTime().getTime() < 30 * 1000) {
            return;
        }

        OrderDelayPush orderDelayPush = new OrderDelayPush();

        orderDelayPush.setTenantCode(orderInfo.getTenantCode());
        orderDelayPush.setTenantOperatorCode(orderInfo.getTenantOperatorCode());
        orderDelayPush.setMemberId(orderInfo.getMemberId());
        orderDelayPush.setElecStationId(orderInfo.getElecStationId());
        orderDelayPush.setElecPileId(orderInfo.getElecPileId());
        orderDelayPush.setElecGunId(orderInfo.getElecGunId());
        orderDelayPush.setStartTime(orderInfo.getStartTime());
        orderDelayPush.setEndTime(new Date());
        orderDelayPush.setOrderNum(orderInfo.getOrderNum());
        orderDelayPush.setOutOrderNum(orderInfo.getOutOrderNum());
        orderDelayPush.setTotalPower(cbdto.getPower());

        //16进制转10进制
        if (cbdto.getElecPileStopReason() != null) {
            orderDelayPush.setPileStopReason(Integer.parseInt(cbdto.getElecPileStopReason(), 16));
        }
        orderDelayPush.setBeginSoc(orderInfo.getBeginSoc());
        orderDelayPush.setPowerA(cbdto.getPowerA());
        orderDelayPush.setPowerB(cbdto.getPowerB());
        orderDelayPush.setSoc(cbdto.getSoc());
        orderDelayPush.setCreatedTime(new Date());

        orderDelayPushMapper.insertSelective(orderDelayPush);
    }
}
